home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-10-24 | 2.1 KB | 83 lines | [TEXT/R*ch] |
- // SmartTextField.java
- // MacTech's Symantec Top Ten, November 1996
-
- class Cell extends Observable implements Observer
- {
- double curValue;
- double oldValue;
- double delta;
-
- SmartTextField theText;
-
- public Cell(SmartTextField aText)
- {
- theText = aText;
- }
-
- public void Update(Observable o, Object arg)
- {
- oldValue = curValue; // We are using a model where the cell is both an
- // observing and observed so if this method is being
- // called then a cell that this object
- // observes has changed.
- curValue += ((Cell)o).delta; // calculate delta
-
- theText.setText(""+curValue); // set the SmartTextField to the new Value
- super.setChanged();
- notifyObservers();
- super.clearChanged();
- }
- }
-
- class SmartTextField extends TextField
- {
- Cell theCell;
-
- public SmartTextField(String someText, int width)
- {
- super(someText, width);
- theCell = new Cell(this);
- }
-
- ////------------ handleEvent ------------/////
- public boolean handleEvent(Event evt)
- {
- // when the cell gets the focus, save its value
- // so we can check if it has changed when it loses the focus
- if(evt.id == evt.GOT_FOCUS)
- {
- ((SmartTextField)evt.target).selectAll();
- ((SmartTextField)evt.target).theCell.oldVal =
- Double.valueOf(((SmartTextField)evt.target)
- .getText()).doubleValue();
- return false;
- }
-
- //lost the focus check to see if the value changed and deal with it
- if(evt.id == evt.LOST_FOCUS)
- {
- //get the value of the current cell
- ((SmartTextField)evt.target).theCell.curVal =
- Double.valueOf(((SmartTextField)evt.target)
- .getText()).doubleValue();
-
- //calculate the delta
- ((SmartTextField)evt.target).theCell.delta =
- ((SmartTextField)evt.target).theCell.curVal -
- ((SmartTextField)evt.target).theCell.oldVal;
-
- //if the delta is non zero, i.e. the value was changed
- if ( ((SmartTextField)evt.target).theCell.delta != 0 )
- {
- ((SmartTextField)evt.target).theCell.setChanged();
- ((SmartTextField)evt.target).theCell.notifyObservers();
- return true;
- }
- else
- return false;
- }
- else
- return false;
- }
- }
-